package jsu.blogger.controller;

import jakarta.servlet.http.HttpServletRequest;
import jsu.blogger.filter.DataFilter;
import jsu.blogger.pojo.Result;
import jsu.blogger.pojo.VerificationCode;
import jsu.blogger.service.impl.LoginAndRegisterServiceImpl;
import jsu.blogger.util.ReturnResultUtil;
import jsu.blogger.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginAndRegisterController {
    private static final Logger log = LoggerFactory.getLogger(LoginAndRegisterController.class);
    @Autowired
    private LoginAndRegisterServiceImpl loginAndRegister;

    @PostMapping("/login")
    public Result login(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("登录失败，检查你的账号密码");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String email= (String) map.get("email");
        String password= (String) map.get("password");
        String currentDevice= (String) map.get("currentDevice");
        //System.out.println("login:" + email + "-" + password + "-" + currentDevice);
        //System.out.println(email);
        //System.out.println(password);
        //System.out.println(currentDevice);
        Long UserId= loginAndRegister.login(email,password,currentDevice);
        if(UserId!=-1){
            String token=TokenUtil.returnToken(UserId,password);
            Map<String,Object> reMap=new HashMap<>();
            reMap.put("UserId",UserId);
            reMap.put("token",token);
            result=ReturnResultUtil.success(reMap);

        }

        return result;
    }

    @PostMapping("/registerGetEmailVerifyCode")
    public Result registerGetEmailVerifyCode(HttpServletRequest request){

        Result result=ReturnResultUtil.failure("该邮箱存在");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String email= (String) map.get("email");
        log.info("收到邮箱注册验证码请求"+email);
        if(loginAndRegister.sendRegisterEmailVerifyCode(email)){
            result=ReturnResultUtil.success("验证码发送成功");
        }
        return result;
    }
    @PostMapping("/getEmailVerifyCode")
    public Result getEmailVerifyCode(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("该邮箱存在");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String email= (String) map.get("email");

        if(loginAndRegister.sendEmailVerifyCode(email)){
            result=ReturnResultUtil.success("验证码发送成功");
        }
        return result;
    }

    @PostMapping("/register")
    public Result register(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("注册失败，请联系管理员");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String email= (String) map.get("email");
        String password=(String) map.get("password");
        String code=(String) map.get("code");
        System.out.println("register:" + email + "-" + password + "-" + code);

        VerificationCode verificationCode=new VerificationCode(email,code, LocalDateTime.now());
        int i=loginAndRegister.Register(email,password,verificationCode);
        if(i==0){
            result=ReturnResultUtil.failure("验证码错误");
        }else if(i==1){
            result=ReturnResultUtil.failure("验证码过期");
        } else if (i == 2) {
            result=ReturnResultUtil.failure("系统错误，请联系我们");
        }else {
            result=ReturnResultUtil.success("注册成功，快去登录吧");
        }
        return result;
    }

    @PostMapping("/changeAccount")
    public Result changeAccount(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("注册失败，请联系管理员");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String email= (String) map.get("email");
        String code=(String) map.get("code");
        System.out.println("register:" + email + "-" + code);

        VerificationCode verificationCode=new VerificationCode(email,code, LocalDateTime.now());
        int i=loginAndRegister.changeAccount(email,verificationCode);
        if(i==0){
            result=ReturnResultUtil.failure("验证码错误");
        }else if(i==1){
            result=ReturnResultUtil.failure("验证码过期");
        } else if (i == 2) {
            result=ReturnResultUtil.failure("系统错误，请联系我们");
        }else {
            result=ReturnResultUtil.success("认证成功");
        }
        return result;
    }

}
